package com.cblz.bd.server.core.handle;

import org.springframework.util.StreamUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * {@code @Name:} ResetRequestWrapper
 * {@code @Description: } request请求体复制
 * {@code @Author:} by yanjieping
 * {@code @CreateDate:} 2022/12/8 - 周四
 */
public class ResetRequestWrapper extends HttpServletRequestWrapper {
    private byte[] requestBody = null;

    public ResetRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        //重点就是这里，把获取到的流数据重新复制给一个对象
        requestBody = StreamUtils.copyToByteArray(request.getInputStream());
    }

    @Override
    public ServletInputStream getInputStream() {
        final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() {
                return bais.read();  // 读取 requestBody 中的数据
            }
        };
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }
}
